// Package _range front_range
// @Title _range
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/1 16:31
// @Create  2021/9/1 16:31
package _range

import "go_leetcode/special_practice/3_tree"

// FrontRange 使用栈实现前序遍历的打印一棵树
func FrontRange(root *__tree.MyTreeNode) []int {
	ans := make([]int, 0)
	if root == nil {
		return ans
	}
	stack := []*__tree.MyTreeNode{root} // 头结点入栈
	for len(stack) > 0 {
		temp := stack[len(stack)-1]
		stack = stack[:len(stack)-1] // 弹出栈的一个元素
		ans = append(ans, temp.Val)
		if temp.Right != nil {
			stack = append(stack, temp.Right)
		}
		if temp.Left != nil {
			stack = append(stack, temp.Left)
		}
	}
	return ans
}
